package com.leyou.personnel.test;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xhh
 * @describe 描述
 * @date 2022/5/18
 */
public class GenerateMybatisPlusTest1 {
    public static void main(String[] args) {
        AutoGenerator mpg=new AutoGenerator();

        //全局配置
        GlobalConfig gc=new GlobalConfig();
        String projectPath=System.getProperty("user.dir");
        System.err.println(projectPath);
        //生成文件位置
        gc.setOutputDir(projectPath+"/back-stage-management-personnel"+"/src/main/java");
        //设置作者
        gc.setAuthor("xhh");
        //生成后是否打开文件夹
        gc.setOpen(false);
        //生成的是否覆盖原来的
        gc.setFileOverride(false);
        //设置服务名
        gc.setServiceName("%sService");
        //设置服务实现类名
        //gc.setServiceImplName();
        //设置主键策略
        gc.setIdType(IdType.AUTO);
        //设置日期类型
        gc.setDateType(DateType.ONLY_DATE);
        //设置swagger文档
        gc.setSwagger2(true);



        mpg.setGlobalConfig(gc);

        //数据源配置
        DataSourceConfig dsc=new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://39.107.125.207:3306/leyou_personnel");
        dsc.setUsername("root").setPassword("nmsl");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");

        mpg.setDataSource(dsc);

        //包配置
        PackageConfig pc=new PackageConfig();
        //模块名
        pc.setModuleName("leyou.personnel");
        //包名
        pc.setParent("com");
        //三层
        //pc.setController("controller1");
        //pc.setService("service1");
        //pc.setMapper("mapper1");

        pc.setEntity("data.entity");

        mpg.setPackageInfo(pc);

        //策略配置
        StrategyConfig sc=new StrategyConfig();
        //设置要映射的表名
        //sc.setInclude("curriculum_generate_rule","curriculum_subject","date_curr_teacher_relation","grade","school_date","school_date_generate_rule","teacher");
        //sc.setInclude("administrators_manager","administrators_role","administrators_role_authority","user_list");
        sc.setInclude("administrators_authority");
        //开启驼峰命名
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        //pojo的父类实体,没有就不用设置!
        //sc.setSuperEntityClass("");
        //自动lombok
        sc.setEntityLombokModel(true);
        //逻辑删除
        sc.setLogicDeleteFieldName("deleted");
        //自动填充配置
        TableFill fill1 = new TableFill("insert_time", FieldFill.INSERT);
        TableFill fill2 = new TableFill("modified_time", FieldFill.INSERT_UPDATE);
        List<TableFill> fillList=new ArrayList<>();
        fillList.add(fill1);
        fillList.add(fill2);
        //填充测率
        sc.setTableFillList(fillList);
        //controller 开启rest风格
        sc.setRestControllerStyle(true);
        //请求url链接参数加_,例如localhost:8080/update/user_name/
        sc.setControllerMappingHyphenStyle(true);
        //开启乐观锁
        //sc.setVersionFieldName("");

        //自定义配置
        InjectionConfig ic= new InjectionConfig() {
            @Override
            public void initMap() {
                //do nothing
            }
        };
        //

        mpg.setStrategy(sc);

        mpg.execute();
    }
}
